package com.wind.taobao.api.trade.taobaoke.convert
{
	import com.wind.taobao.api.trade.item.model.TaobaoItem;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;


	/**
	 * 淘宝客商品转换器
	 * @author fxy
	 *
	 */
	public class TaobaokeItemConvert
	{
		/**
		 * 转换Item集合
		 */
		public static function convertTaobaokeItems(value:Object, type:String="json"):ArrayCollection
		{
			var items:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							items.addItem(convertTaobaokeItem((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return items;
		}

		/**
		 * 转换单个Item对象
		 * value:XML 或 JSON
		 */
		public static function convertTaobaokeItem(value:Object, type:String="json"):TaobaoItem
		{
			var item:TaobaoItem=new TaobaoItem();
			if (value == null)
				return item;
			if (type == TaobaoConstants.XML)
			{
				item=convertXMLToTaobaokeItem(XML(value), item);
			}
			else
			{
				item=convertJSONToTaobaokeItem(value, item);
			}
			return item;
		}

		/**
		 * JSON转换为Item
		 */
		private static function convertJSONToTaobaokeItem(json:Object, item:TaobaoItem):TaobaoItem
		{
			if (json.hasOwnProperty(TaobaoConstants.ID))
			{
				item.iid=json[TaobaoConstants.ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.CLICK_URL))
			{
				item.clickUrl=json[TaobaoConstants.CLICK_URL];
			}
			if (json.hasOwnProperty(TaobaoConstants.TITLE))
			{
				item.title=json[TaobaoConstants.TITLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.NICK))
			{
				item.nick=json[TaobaoConstants.NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.PICT_URL))
			{
				item.picUrl=json[TaobaoConstants.PICT_URL];
			}
			if (json.hasOwnProperty(TaobaoConstants.PRICE))
			{
				item.price=json[TaobaoConstants.PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMMISSION))
			{
				item.commission=json[TaobaoConstants.COMMISSION];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMMISSION_NUM))
			{
				item.commission_num=json[TaobaoConstants.COMMISSION_NUM];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMMISSION_RATE))
			{
				item.commission_rate=json[TaobaoConstants.COMMISSION_RATE];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMMISSION_VOLUME))
			{
				item.commission_volume=json[TaobaoConstants.COMMISSION_VOLUME];
			}
			return item;
		}



		/**
		 * XML转换为Item
		 */
		private static function convertXMLToTaobaokeItem(value:XML, item:TaobaoItem):TaobaoItem
		{
			return item;
		}

	}
}